//
//  HELSigninVC.m
//  HandheldELearn
//
//  Created by 陈武琦 on 16/9/22.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "HELSigninVC.h"
#import "HELSigninCell.h"
#import "HELWebViewController.h"
#import "HELSuperTableView.h"
#import "Masonry.h"
#import "HELSigninDetailVC.h"
#import <UITableView+FDTemplateLayoutCell.h>

static NSString *cellID = @"HELSigninCellID";
@interface HELSigninVC ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong)HELSuperTableView *tableView;
//上拉加载的页面
@property (nonatomic, assign) NSInteger page;

@end

@implementation HELSigninVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"HELSigninCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:cellID];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self titleView:@"签到课程"];
//    self.dataArray = [[NSMutableArray alloc] init];
    weakSelf_define;
    ///*
    // 添加下拉刷新头部控件
    [self.tableView wsj_HeaderRefreshBlock:^{
        //请求加载数据
        weakSelf.page = 1;
        [weakSelf requestData];
    }];
    
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        weakSelf.page++;
        //请求加载数据
        [weakSelf requestData];
    }];
    self.page = 1;
    [self requestData];
    // */
}

- (void)requestData {
  ///*
    HELUserInfoM *userInfo = [HELAppManager userInfo];
    if (!userInfo.userId) {
        alertMessage(@"用户id获取失败");
        return;
    }
    if (!userInfo.hospitalId) {
        alertMessage(@"用户医院获取失败");
        return;
    }
    self.tableView.loading = YES;
    NSDictionary *param = @{@"pageSize":@5, @"currentPage":@(self.page), @"signUser":userInfo.userId, @"hospitalId":userInfo.hospitalId};
    defineWeakSelf;
    [[HELNetworkManager sharedMg] POST:helCheckUserSign parameters:param success:^(id responseObject) {
        if (responseObject[@"exception"]) {
            cwqHudRequestError(responseObject[@"exception"]);
        }else {
            NSMutableArray *arr = [NSMutableArray array];
            for (NSDictionary *dict in responseObject[@"rows"]) {
                HELSigninCourseModel *model = [HELSigninCourseModel mj_objectWithKeyValues:dict];
                [arr addObject:model];
            }
            //判断是上拉刷新？  还是下拉加载？
            if (weakSelf.page > 1) {
                NSMutableArray *muArr = [weakSelf.dataArray mutableCopy];
                
                [muArr addObjectsFromArray:arr];
                weakSelf.dataArray = [muArr copy];
                if (weakSelf.page == [responseObject[@"pages"] integerValue]) {
                    weakSelf.tableView.footer.hidden = YES;
                }
                [weakSelf.tableView.footer endRefreshing];
            }else {
                
                if (weakSelf.page == [responseObject[@"pages"] integerValue]) {
                    weakSelf.tableView.footer.hidden = YES;
                }else {
                    weakSelf.tableView.footer.hidden = NO;
                }
                weakSelf.dataArray = arr;
                [weakSelf.tableView.header endRefreshing];
            }
            if (weakSelf.dataArray.count<=0) {
                weakSelf.tableView.loading = NO;
            }
            
            [weakSelf.tableView reloadData];
            
        }
    } failure:^(NSString *err) {
        if (weakSelf.page > 1) {
            //上拉
            [weakSelf.tableView.footer endRefreshing];
            weakSelf.page -= 1;
        }else {
            //下拉
            [weakSelf.tableView.header endRefreshing];
            if (weakSelf.dataArray.count<=0) {
                self.tableView.footer.hidden = YES;
            }else {
                
                self.tableView.footer.hidden = NO;
            }
        }
        cwqHudRequestError(err);
        if (weakSelf.dataArray.count<=0) {
            weakSelf.tableView.loading = NO;
        }
        
    }];
    
    //*/
}



- (HELSuperTableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[HELSuperTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
        _tableView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:_tableView];
    }
    return  _tableView;
}
#pragma mark - <UITableViewData and UITableDelegate>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    ///*
    HELSigninCourseModel *model = self.dataArray[indexPath.row];
    CGFloat height = [tableView fd_heightForCellWithIdentifier:cellID configuration:^(HELSigninCell *cell) {
        cell.model = model;
    }];
    return height;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HELSigninCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    HELSigninCourseModel *model = self.dataArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setModel:model];
    [cell setColorIndex:indexPath.row%7];
    return cell;
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HELSigninDetailVC *signinDetailVC = [[HELSigninDetailVC alloc] initWithModel:self.dataArray[indexPath.row]];
    [self.navigationController pushViewController:signinDetailVC animated:YES];
        
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end


